export class Msg {

	/**
	 * confirm
	 * @param content 显示文字内容
	 * @param title 显示标题
	 * @returns 确定: true, 取消: false
	 */
	static confirm = (content, title = '系统提示') => {
		return new Promise((resolve) => {
			uni.showModal({
				title,
				content,
				showCancel: true,
				cancelText: '取消',
				confirmText: '确定',
				success: (res) => {
					if (res.confirm) {
						resolve(true);
					} else if (res.cancel) {
						resolve(false);
					}
				},
				fail: () => { },
				complete: () => { }
			});
		});
	}


	static input = (content, title = '系统提示') => {
		let result = '';
		return new Promise((resolve) => {
			uni.showModal({
				title,
				content,
				editable: true,
				showCancel: true,
				cancelText: '取消',
				confirmText: '确定',
				success: (res) => {
					if (res.confirm) {
						result = res.content;
						resolve(result);
					} else if (res.cancel) {
						result = '';
						resolve(result);
					}
				},
				fail: () => { },
				complete: () => { }
			});
		});
	}


	/**
	 * alert
	 * @param content 显示文字内容
	 * @param title 显示标题
	 * @returns 
	 */
	static alert = (content, title = '系统提示') => {
		return new Promise((resolve) => {
			uni.showModal({
				title,
				content: content + '',
				showCancel: false,
				confirmText: '确定',
				success: () => { },
				fail: () => { },
				complete: () => {
					resolve(undefined);
				}
			});
		});
	}

	/**
	 * toast 
	 * @param content 显示文字内容
	 * @param duration 显示时长(ms)
	 * @returns 
	 */
	static toast = (content, duration = 1000) => {
		uni.hideToast();
		return new Promise((resolve) => {
			let result = true;
			uni.showToast({
				title: content,
				position: 'bottom',
				mask: true,
				icon: 'none',
				duration,
				success: () => {
					result = true;
				},
				fail: () => {
					result = false;
				},
				complete: () => {
				},
			});
			setTimeout(() => {
				resolve(result);
			}, duration);
		});
	}


	static action = (itemList) => {
		return new Promise((resolve) => {
			uni.showActionSheet({
				itemList: itemList,
				success: function (res) {
					resolve(res.tapIndex);
				},
				fail: function (res) {
					resolve(-1);
				}
			});
		});
	};

	/**
	 * 显示加载 
	 * @param content 显示文字内容
	 * @returns 
	 */
	static loading = (content) => {
		// uni.hideLoading();
		return new Promise((resolve) => {
			uni.showLoading({
				title: content,
				mask: true,
				success: () => { },
				fail: () => { },
				complete: () => {
					resolve(undefined);
				},
			});
		});
	}


	/**
	 * 关闭 loading
	 * @returns 
	 */
	static hideLoading = () => {
		if (uni.canIUse('uni.hideLoading')) {
			try {
				uni.hideLoading();
			} catch {

			}
		}
		return;
	}

}